﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace TimKen.DataAccess
{
    /// <summary>
    /// 数据访问类POptionRelPType。
    /// </summary>
    public class POptionRelPTypeDAO
    {
        public POptionRelPTypeDAO()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int OptionID, int TypeID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@OptionID", SqlDbType.Int,4),
					new SqlParameter("@TypeID", SqlDbType.Int,4)};
            parameters[0].Value = OptionID;
            parameters[1].Value = TypeID;

            int result = DbHelperSQL.RunProcedure("UP_POptionRelPType_Exists", parameters, out rowsAffected);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public void Add(TimKen.DataEntity.POptionRelPType model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@OptionID", SqlDbType.Int,4),
					new SqlParameter("@TypeID", SqlDbType.Int,4),
					new SqlParameter("@AttributeIndex", SqlDbType.Bit,1)};
            parameters[0].Value = model.OptionID;
            parameters[1].Value = model.TypeID;
            parameters[2].Value = model.AttributeIndex;

            DbHelperSQL.RunProcedure("UP_POptionRelPType_ADD", parameters, out rowsAffected);
        }

        /// <summary>
        ///  更新一条数据
        /// </summary>
        public void Update(TimKen.DataEntity.POptionRelPType model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@OptionID", SqlDbType.Int,4),
					new SqlParameter("@TypeID", SqlDbType.Int,4),
					new SqlParameter("@AttributeIndex", SqlDbType.Bit,1)};
            parameters[0].Value = model.OptionID;
            parameters[1].Value = model.TypeID;
            parameters[2].Value = model.AttributeIndex;

            DbHelperSQL.RunProcedure("UP_POptionRelPType_Update", parameters, out rowsAffected);
        }

        public void UpdateSortOrder(string typeID, string optionID, int sortOrder) 
        {
            int rowsAffected;
            SqlParameter[] parameters ={
                                           new SqlParameter("@OptionID", SqlDbType.Int,4),
                                           new SqlParameter("@TypeID", SqlDbType.Int,4),
                                           new SqlParameter("@SortOrder", SqlDbType.Int,4)
                                      };
            parameters[0].Value = optionID;
            parameters[1].Value = typeID;
            parameters[2].Value = sortOrder;
            rowsAffected= DbHelperSQL.ExecuteSql("update POptionRelPType set SortOrder=@Sortorder where TypeID=@TypeID and OptionID=@OptionID",parameters);
        }

        /// <summary>
        /// 删除多条数据
        /// </summary>
        public void Delete(string OptionIDs, string TypeID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@OptionID", SqlDbType.VarChar,100),
					new SqlParameter("@TypeID", SqlDbType.VarChar,20)};
            parameters[0].Value = OptionIDs;
            parameters[1].Value = TypeID;

            DbHelperSQL.RunProcedure("UP_POptionRelPType_Delete", parameters, out rowsAffected);
        }



        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TimKen.DataEntity.POptionRelPType GetModel(int OptionID, int TypeID)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@OptionID", SqlDbType.Int,4),
					new SqlParameter("@TypeID", SqlDbType.Int,4)};
            parameters[0].Value = OptionID;
            parameters[1].Value = TypeID;

            TimKen.DataEntity.POptionRelPType model = new TimKen.DataEntity.POptionRelPType();
            DataSet ds = DbHelperSQL.RunProcedure("UP_POptionRelPType_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["OptionID"].ToString() != "")
                {
                    model.OptionID = int.Parse(ds.Tables[0].Rows[0]["OptionID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TypeID"].ToString() != "")
                {
                    model.TypeID = int.Parse(ds.Tables[0].Rows[0]["TypeID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AttributeIndex"].ToString() != "")
                {
                    model.AttributeIndex = bool.Parse(ds.Tables[0].Rows[0]["AttributeIndex"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select OptionID,TypeID,AttributeIndex ");
            strSql.Append(" FROM POptionRelPType ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion  成员方法
    }
}
